<script setup>
import FirstTitle from "@comp/FirstTitle";
import { Editor, Toolbar } from "@wangeditor/editor-for-vue";
import { onMounted } from "vue";
import { searchDataRyfh } from "@/api/search";
import { addOrUpdateRyfh } from "@/api/addOrUpdate";

const city = window.globalObj.name;
const editorRef = shallowRef();

const toolbarConfig = {};
const editorConfig = {
  placeholder: "请输入内容...",
};
const mode = ref("default");

// 加载html
const initHtml = () => {
  searchDataRyfh({ m: "相关保障-掩蔽场所保障要点" }).then((res) => {
    const { data } = res;
    valueHtml.value = data.find((x) => x.k === "掩蔽场所保障要点")?.v || "";
  });
};

const handleCreated = (editor) => {
  editorRef.value = editor; // 记录 editor 实例，重要！
};

const valueHtml = ref(
  "保障任务：依据命令快速保障留城人员的掩蔽行动。<br/>保障部署：按照就地就近分配掩蔽工程和地域，人防工程的分配使用，在优先保证各级指挥、通信和防空专业队伍掩（隐）蔽使用的前提下，原则上各单位自建的人防工程自行使用，公用人防工程由市、区、街道按照统筹兼顾、突出重点的原则，统一分配使用。各县（区）人防指挥部结合本级留城人口实际，制定人员掩蔽实施方案计划，以先人防工程、后掩蔽场所和应急避难场所的顺序组织人员掩蔽行动。<br/>掩蔽场所区分：（见附件5：《某某市掩蔽场所分配表》）。<br/>人防工程区分：（见附件6：《某某市人防工程分配表》）。<br/>保障方法：逐级分块组织，主要由街道和社区分块组织为主，以机关、院校、工厂、商场成系统组织为辅。"
);

const saveProgramme = () => {
  ElMessageBox.confirm(`确定要保存吗?`, "警告", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
    customClass: "message-box",
  }).then(() => {
    addOrUpdateRyfh({
      m: "相关保障-掩蔽场所保障要点",
      data: [
        {
          k: "掩蔽场所保障要点",
          v: valueHtml.value,
        },
      ],
    })
      .then(() => {
        ElMessage.success("操作成功");
      })
      .catch(() => {});
  });
};

onMounted(() => {
  initHtml();
});
onBeforeUnmount(() => {
  const editor = editorRef.value;
  if (!editor) return;
  editor.destroy();
});
</script>
<template>
  <div class="">
    <FirstTitle name="掩蔽场所保障要点" />
    <div style="border: 1px solid #ccc" class="top-15">
      <Toolbar
        style="border-bottom: 1px solid #ccc"
        :editor="editorRef"
        :defaultConfig="toolbarConfig"
        :mode="mode"
      />
      <Editor
        style="height: 500px; overflow-y: hidden"
        v-model="valueHtml"
        :defaultConfig="editorConfig"
        :mode="mode"
        @onCreated="handleCreated"
      />
    </div>

    <div class="flex-justify-end top-20">
      <el-button type="primary" class="save-btn" @click="saveProgramme"
        >保存</el-button
      >
    </div>
  </div>
</template>
<style scoped lang="scss"></style>